package com.shop.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.shop.entity.SHOP_PRODUCT;
import com.shop.service.SHOP_PRODUCTDao;

/**
 * Servlet implementation class DoProductUpdate
 */
@WebServlet("/manage/admin_doproductupdate")
public class DoProductUpdate extends HttpServlet {
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html charset=utf-8");
		//创建SmartUpload对象
		SmartUpload su= new SmartUpload();
		
		//初使化
		su.initialize(this.getServletConfig(), request, response);
		
		//上传过程
		try {
			su.upload();
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		String pname = request.getParameter("productName");
		String id = request.getParameter("parentId");
		String price = request.getParameter("productPrice");
		String desc = request.getParameter("productDesc");
		String stock = request.getParameter("productStock");
		
		//获取上传的文件对象
		Files fs= su.getFiles();
		File f = fs.getFile(0);
		//获取上传的文件名称
		String fname = f.getFileName();
		
	    PrintWriter pw=response.getWriter(); 			//连接数据库 			
		//创建用户实体
		SHOP_PRODUCT product=new SHOP_PRODUCT(
				0, 
				pname,
				desc,
				Integer.parseInt(price),
				Integer.parseInt(stock),
				Integer.parseInt(id.split("-")[0]),
				Integer.parseInt(id.split("-")[1]),
				fname
				);


		int count = SHOP_PRODUCTDao.update(product);
		//System.out.print(u);
		//成功或失败的去向
		if(count > 0) {
			response.sendRedirect("/shop/manage/admin_douserselect?cp="+request.getParameter("cpage"));
			
		}else {
			PrintWriter out = response.getWriter();
			out.write("<script>");
			out.write("alter('用户登录失败')");
			out.write("location.href='manage/admin_touserupdate");
			out.write("</script>");
		  
	}

		
	}

}

